package com.example.server.web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.server.web.domain.RespBean;
import com.example.server.web.domain.Role;
import com.example.server.web.domain.page.PageQueryDto;
import com.example.server.web.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2023-04-04
 */
@Api(tags="角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @ApiOperation("分页获取角色列表")
    @PostMapping("/list")
    public RespBean getRolesList(@Validated @RequestBody PageQueryDto pageQueryDto){
        Page<Role> page = roleService.getRolesPage(pageQueryDto);
        List<Role> rolesList = page.getRecords();
        Long total = page.getTotal();
        HashMap<String,Object> ans = new HashMap<>();
        ans.put("rolesList",rolesList);
        ans.put("total",total);
        return RespBean.success("查询成功",ans);
    }

    @ApiOperation("根据id获取角色信息")
    @GetMapping("/select/{roleId}")
    public RespBean getRoleById(@PathVariable("roleId") Long roleId){
        Role role = roleService.getRoleById(roleId);
        return RespBean.success("查询成功",role);
    }

    @ApiOperation("增加角色")
    @PostMapping("/add")
    public RespBean insertRole(@Validated @RequestBody Role role){
        Integer ans = roleService.insertRole(role);
        return RespBean.success("增加成功",ans);
    }

    @ApiOperation("更新角色信息")
    @PutMapping("/update")
    public RespBean updateRole(@Validated @RequestBody Role role){
        Integer ans = roleService.updateRole(role);
        return RespBean.success("更新成功",ans);
    }

    @ApiOperation("删除角色信息")
    @DeleteMapping ("/delete/{roleId}")
    public RespBean deleteRole(@PathVariable("roleId") Long roleId){
        Integer ans = roleService.deleteRole(roleId);
        return RespBean.success("删除成功",ans);
    }

    @ApiOperation("批量删除角色信息")
    @DeleteMapping ("/deletes")
    public RespBean deleteRole(@RequestBody List<Long> roleIdList){
        Integer ans = roleService.deleteRoles(roleIdList);
        return RespBean.success("删除成功",ans);
    }



}
